package com.sec.etech.ism.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sec.etech.common.constant.DBConstant;
import com.sec.etech.ism.constant.SignatureConstant;
import com.sec.etech.ism.service.IsmCommonService;
import com.sec.etech.ism.service.IsmMethodService;
import com.sec.etech.ism.service.IsmTaskService;
import com.sec.etech.sys.util.DbUtil;
import lombok.extern.slf4j.Slf4j;
import org.openbpm.base.api.exception.BusinessMessage;
import org.openbpm.base.core.util.StringUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class IsmGetNcFileAttachmentsServiceImpl implements IsmMethodService {
    @Resource
    IsmCommonService ismCommonService;
    @Resource
    IsmTaskService ismTaskService;

    @Override
    public JSONObject call(String sysCode, String serviceCode, JSONObject businessParams) throws Exception {
        JSONObject result = new JSONObject();
        String ncNum = this.checkAndGetData(businessParams);
        JSONObject resData = this.getNcFileAttachments(ncNum);
        ismTaskService.deleteTaskByParams(sysCode, serviceCode, serviceCode, null,
                businessParams.getString(SignatureConstant.INPUT_PARAMS));
        result.put("flag", "success");
        result.put("data", resData);
        return result;
    }

    //入参检查
    private String checkAndGetData(JSONObject busParams) throws BusinessMessage {
        JSONObject object = busParams.getJSONObject(SignatureConstant.INPUT_PARAMS);
        String ncNum = object.getString("ncNum");
        if (StringUtil.isEmpty(ncNum)) {
            throw new BusinessMessage("请求中缺少ncNum参数，请检查！");
        }
        return ncNum;
    }

    private JSONObject getNcFileAttachments(String ncNum) throws Exception {

        JdbcTemplate slaveJdbcTemplate = DbUtil.getJdbcTemplate(DBConstant.ETECH_SLAVE);
        String sql = "SELECT t1.ncid,t1.ncnum,t1.versions,t2.ncnum commonNcNum " +
                "FROM proc_nc t1 left join proc_nc t2 on t2.ncid=t1.commonncid " +
                "where t1.active=1 and t1.ncnum=?";
        List<Map<String, Object>> ncList = slaveJdbcTemplate.queryForList(sql, ncNum);
        if (ncList == null || ncList.size() == 0) {
            return null;
        }
        JSONObject dataObj = new JSONObject();
        //dataObj.put("commonNcNum", ncList.get(0).get("commonNcNum"));//通用程序编号
        String commonNcNum = ncList.get(0).get("commonNcNum") != null ? ncList.get(0).get("commonNcNum").toString() : null;
        if (commonNcNum == null || StringUtil.isEmpty(commonNcNum)) {
            String versions = ncList.get(0).get("versions").toString();
            //{"0":"无","1":"常规清单","2":"常规程序","3":"主头清单","4":"主头程序","5":"副头清单",
            // "6":"副头程序","7":"常规附件","8":"主头附件","9":"副头附件"}
            sql = "select t2.id_,t2.name_,t2.path_,t2.preview_,t2.module_,t2.uploader_, t1.typeid " +
                    "from proc_nc_files t1, sys_file t2 " +
                    "where t1.uploadseries=t2.id_ and t1.typeid=7 and t1.ncid=? and t1.versions=?";
            List<Map<String, Object>> fileList = slaveJdbcTemplate.queryForList(sql, ncList.get(0).get("ncid"), versions);
            if (fileList != null && fileList.size() > 0) {
                //zip文件名称 常规程序
                StringBuffer zipFileName = new StringBuffer("MES用附加文件_").append(ncList.get(0).get("ncNum").toString())
                        .append("_").append(versions).append(".zip");
                String sharedFileAddr = ismCommonService.getZipFile(fileList, zipFileName.toString(), 7);
                dataObj.put("sharedFileAddr", sharedFileAddr);
            } else {
                dataObj.put("sharedFileAddr", "");
            }
        } else {
            dataObj.put("sharedFileAddr", "");
        }
        return dataObj;
    }

    @Override
    public String methodName() {
        return "getNcFileAttachments";
    }
}
